﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Security;
namespace Common
{

    public class ReceiveThread : MessageLooperBase
        {
            SslStream _dis;
            public delegate void OnDataReceiveHandler(DataPackage data);
            public event OnDataReceiveHandler OnDataReceived;


            public ReceiveThread(SslStream dis)
            {

                _dis = dis;
            }
            protected override void handleItem(WorkItem anItem)
            {
                DataPackage currentPakcage = anItem.data as DataPackage;
                if (!currentPakcage.read(_dis))
                {
                    OnNetowrkFail();
                }
                StartNextRead();
                OnDataReceived(currentPakcage);
            }
            public override void OnExited()
            {
                return;
            }
            public override void start()
            {
                base.start();
                StartNextRead();

            }
            void StartNextRead()
            {
                DataPackage newPackage = new DataPackage();
                WorkItem newItem = new WorkItem();
                newItem.data = newPackage;
                QueueItem(newItem);

            }
            public delegate void NetworkFailHandler();
            public event NetworkFailHandler OnNetowrkFail;
        }
   

}
